unit queuetest;

{$IFDEF FPC}
  {$MODE Delphi}{$H+}
{$ENDIF}

interface

uses
  Classes, SysUtils, sets;

type

  TProducer = class(TThread)
    Count, Value: Integer;
    constructor Create(aCount, aValue: Integer);
    procedure Execute; override;
  end;

  TConsumer = class(TThread)
    Count, Total: Integer;
    constructor Create(aCount: Integer);
    procedure Execute; override;
  end;

var
  Q:IQueue;

implementation

{ TProducer }

constructor TProducer.Create(aCount, aValue: Integer);
begin
  Count := aCount;
  Value := aValue;
  inherited Create(False);
end;

procedure TProducer.Execute;
var
  i: Integer;
begin
  { Insere [Count] integers na Queue }
  for i := 1 to Count do
    Q.Push(TObject(Value));
end;

{ TConsumer }

constructor TConsumer.Create(aCount: Integer);
begin
  Count := aCount;
  inherited Create(False);
end;

procedure TConsumer.Execute;
var
  i: Integer;
begin
  { Remove os integers da Queue, adicionando em total }
  Total := 0;
  for i := 1 to Count do
    Total := Total + Integer(Q.Pop);
end;

initialization
  //Q := SemQueue(1);
  Q := SemQueue(1024);

end.

